<template>
	<view class="container">
		<view class="tab-wrap">
			<view class="tab-item" @tap='setTimeSort'>
				时间
				<view class="trigle-wrap">
					<view class="trigle-up" :class="[timeBtnObj.timeSort == 'asc' ? 'trigle-up-act':'']"></view>
					<view class="trigle-down" :class="[timeBtnObj.timeSort == 'desc' ? 'trigle-down-act':'']">
					</view>
				</view>
				<!-- <image src='/static/up.png' class="sanjiao" v-if="timeBtnObj.timeSort == 'asc'"></image>
				<image src='/static/down.png' class="sanjiao" v-else></image> -->
			</view>
			<view class="tab-item" @tap='setPriceSort'>
				价格
				<view class="trigle-wrap">
					<view class="trigle-up" :class="[priceBtnObj.priceSort == 'asc' ? 'trigle-up-act':'']"></view>
					<view class="trigle-down" :class="[priceBtnObj.priceSort == 'desc' ? 'trigle-down-act':'']">
					</view>
				</view>
				<!-- <image src='/static/up.png' class="sanjiao" v-if="priceBtnObj.priceSort == 'asc'"></image>
				<image src='/static/down.png' class="sanjiao" v-else></image> -->
			</view>
			<view class="tab-item">
				<picker @change="bindPickerChange" :value="optionObj.type" :range="optionObj.option">
					筛选<image src='/static/shaixuan.png' class="shaixuan"></image>
				</picker>
			</view>
		</view>
		<view class="wrap" v-if='showPage'>
			<view class="list-wrap" v-if='list.length > 0'>
				<view class="list-item" v-for="(item,index) in list" :key="index">
					<view class="title">急需贡献值{{item.contribution}}点</view>
					<view class="tip">急需贡献值{{item.contribution}}点</view>
					<view class="time-wrap">
						<view class="time">
							发布时间：{{item.publish_time}}
						</view>
						<button class="btn line-center" :disabled="item.is_disabled" :loading="item.is_disabled" @tap='receiveTask(index)'>{{item.btn_text}}</button>
					</view>
				</view>
				<loading :loadEnd='loadEnd' class="load-t"></loading>
			</view>
			<no-data v-else class="noData"></no-data>
		</view>
		<modal :showModal="showModal">
			<view class="success-wrap animated" :class="[showModal ? 'zoomIn':'zoomOut']">
				<image src="/static/fabu-icon.png"></image>
				<view class="tip">领取成功</view>
				<view class="tip2">你可在“<text>订单管理</text>”里查看相关信息</view>
				<uni-icons type="close" size="40" class="close" @tap='close' color="#DEDBDB"></uni-icons>
				<!-- <view class="know line-center" @tap='close'>我知道了</view> -->
			</view>
		</modal>
	</view>
</template>

<script>
	const app = getApp()
	import modal from '@/components/modal/modal.vue'
	export default {
		components: {
			modal,
		},
		data() {
			return {
				showPage: false,
				optionObj:{
					option: ['普通', '加急'],
					type:0,
					text:'筛选'
				},
				type:0,
				timeBtnObj: {
					isFlag: false,
					timeSort: ''
				},
				priceBtnObj: {
					isFlag: false,
					priceSort: ''
				},
				showModal: false,
				page: 1,
				totalPage: 1,
				list: [],
				loadEnd: false,
			};
		},
		onLoad() {
			this.getList()
		},
		onReachBottom() {
			let that = this;
			if (that.page < that.totalPage) {
				this.loadEnd = false;
				that.page++;
				that.getList()
			} else {
				return;
			}
		},
		onPullDownRefresh() {
			this.clear();
			uni.stopPullDownRefresh()
		},
		methods: {
			getList() {
				const that = this;
				app.globalRequest({
					api: app.getApi().task.taskBuyListHall,
					data: {
						time_order: this.timeBtnObj.timeSort,
						money_order: this.priceBtnObj.priceSort,
						type: this.type,
						page: that.page,
						limits: 15
					}
				}).then(res => {
					this.showPage = true;
					if (res.code == 0) {
						res.data.data.forEach(v=>{
							v.is_disabled = false;
							v.btn_text = '立即领取'
						})
						that.list = that.list.concat(res.data.data);
						that.totalPage = res.data.last_page;
						if (that.totalPage == that.page) {
							that.loadEnd = true;
						}
					}
				})
			},
			clear(){
				this.list = [];
				this.page = 1;
				this.totalPage = 1;
				this.loadEnd = false;
				this.showPage = false;
				this.getList()
			},
			setTimeSort() {
				let timeSort = this.timeBtnObj.timeSort,
					isFlag = this.timeBtnObj.isFlag;
				isFlag = !isFlag;
				if (isFlag) {
					timeSort = 'asc'
				} else {
					timeSort = 'desc'
				}
				this.timeBtnObj.isFlag = isFlag;
				this.timeBtnObj.timeSort = timeSort;
				this.priceBtnObj.isFlag = false;
				this.priceBtnObj.priceSort = '';
				this.clear()
			},
			setPriceSort() {
				let priceSort = this.priceBtnObj.priceSort,
					isFlag = this.priceBtnObj.isFlag;
				isFlag = !isFlag;
				if (isFlag) {
					priceSort = 'asc'
				} else {
					priceSort = 'desc'
				}
				this.priceBtnObj.isFlag = isFlag;
				this.priceBtnObj.priceSort = priceSort;
				this.timeBtnObj.isFlag = false;
				this.timeBtnObj.timeSort = '';
				this.clear()
			},
			bindPickerChange: function(e) {
				this.optionObj.type = e.detail.value;
				if(e.detail.value <= 1){
					this.type = e.detail.value + 1;
				}else{
					this.type = 0;
				}
				this.optionObj.text = this.optionObj.option[this.optionObj.type];
				this.clear()
			},
			receiveTask(index){
				let list = this.list;
				list[index].is_disabled = true;
				list[index].btn_text = '领取中...';
				app.globalRequest({
					api:app.getApi().task.receiveBuyTask,
					method:"POST",
					data:{
						task_id:list[index].task_id
					}
				}).then(res=>{
					if(res.code == 0){
						list[index].is_disabled = false;
						list[index].btn_text = '立即领取';
						this.showModal = true;
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						list[index].is_disabled = false;
						list[index].btn_text = '立即领取';
					}
					this.list = list;
				})
			},
			close() {
				this.showModal = false;
				this.clear()
			}
		}
	}
</script>
<style>
	page {
		width: 100%;
		height: 100%;
		background: #F6F6F6;
	}
</style>
<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
		background: #F6F6F6;
	}

	.wrap {
		padding-top: 124rpx;
		padding-bottom: 100rpx;
	}

	.tab-wrap {
		width: 100%;
		height: 88rpx;
		background: #FFFFFF;
		padding: 0 48rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-around;
		border-top: 1px solid #EFEFEF;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 22;

		.tab-item {
			display: flex;
			align-items: center;
			.trigle-wrap {
				margin-top: 5rpx;
				margin-left: 10rpx;
			
				.trigle-up {
					width: 0;
					height: 0;
					border-top: 4px solid transparent;
					border-right: 3px solid transparent;
					border-left: 3px solid transparent;
					border-bottom: 4px solid #CCCCCC;
				}
			
				.trigle-up-act {
					border-top: 4px solid transparent;
					border-right: 3px solid transparent;
					border-left: 3px solid transparent;
					border-bottom: 4px solid #707070;
				}
			
				.trigle-down {
					width: 0;
					height: 0;
					border-top: 4px solid #CCCCCC;
					border-right: 3px solid transparent;
					border-left: 3px solid transparent;
					border-bottom: 4px solid transparent;
					margin-top: 5rpx;
				}
			
				.trigle-down-act {
					border-top: 4px solid #707070;
					border-right: 3px solid transparent;
					border-left: 3px solid transparent;
					border-bottom: 4px solid transparent;
				}
			}
			.sanjiao {
				width: 12rpx;
				height: 18rpx;
				position: relative;
				top: 4rpx;
				margin-left: 10rpx;
			}

			.shaixuan {
				width: 20rpx;
				height: 20rpx;
				position: relative;
				top: 4rpx;
				margin-left: 10rpx;
			}
		}
	}

	.list-wrap {
		margin: 0 24rpx;
	}

	.list-item {
		width: 100%;
		height: 174rpx;
		background: url(@/static/list-bg.png) no-repeat;
		background-size: 100% 100%;
		padding: 16rpx 24rpx;
		box-sizing: border-box;
		margin-bottom: 24rpx;

		.title {
			font-weight: bold;
			font-size: 28rpx;
			position: relative;
			padding-left: 14rpx;

			&:before {
				content: '';
				width: 6rpx;
				height: 28rpx;
				background: #000000;
				border-radius: 4rpx;
				position: absolute;
				left: 0;
				top: 0;
				bottom: 0;
				margin: auto;
			}
		}

		.tip {
			font-size: 24rpx;
			color: #808080;
			padding-top: 16rpx;
			padding-left: 14rpx;
		}

		.time-wrap {
			margin-left: 14rpx;
			display: flex;
			align-items: flex-end;
			justify-content: space-between;

			.time {
				font-size: 24rpx;
			}

			.btn {
				width: 136rpx;
				height: 48rpx;
				background: #000000;
				border-radius: 24rpx;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}

	.success-wrap {
		width: 600rpx;
		height: 432rpx;
		background: url(@/static/success-bg.png) no-repeat;
		background-size: 100% 100%;
		position: fixed;
		left: 0;
		top: 456rpx;
		right: 0;
		margin: auto;

		image {
			width: 113rpx;
			height: 111rpx;
			display: block;
			margin: 88rpx auto 0;
		}

		.tip {
			font-weight: bold;
			font-size: 32rpx;
			margin-top: 36rpx;
			text-align: center;
		}

		.tip2 {
			text-align: center;
			font-weight: bold;
			margin-top: 38rpx;

			text {
				color: #FF7A65;
			}
		}

		.know {
			width: 260rpx;
			height: 72rpx;
			background: #000000;
			border-radius: 36rpx;
			color: #fff;
			margin: 50rpx auto 0;
		}

		.close {
			position: absolute;
			right: 16rpx;
			top: 16rpx;
		}
	}
</style>